#! /usr/bin/perl 

# date: 2009-08-02
# download pic 

use strict;
use warnings;
#use utf8;
#use Encode;
#use POSIX qw(strftime);

use threads;
use WWW::Mechanize;
use Thread::Semaphore;
use threads::shared;

$| = 1;

my $pager = shift || 1;
my $url = "http://rr.pstang.com/thread.php?fid=39&search=&page=$pager";

my $mech  = WWW::Mechanize->new(quiet => 1, onerror => undef, timeout => 60);
#$mech->agent_alias('Windows IE 6');
$mech->agent_alias('Linux Mozilla');
$mech->stack_depth(2);
$mech->get($url);
my $content = $mech->content;

print "Begining ...\n";
my $mutux = Thread::Semaphore->new( 40 );
my %url;

#$content = encode("utf-8", decode("gb2312", $content));

while ( $content =~ m{read\.php\?tid=(\d+)' id=''}g ){
    my $tid = $1;
    ## 帖子链接
    my $thread_url = "http://rr.pstang.com/read.php?tid=$tid";
    ## 下载
    $mech->get( $thread_url );
    my $t_content = $mech->content;

    #$t_content = encode("utf-8", decode("gb2312", $t_content));
    if ($t_content =~ m{本主题帖为未审核新贴}) {
        print "can not open\n";
    }

    print "$thread_url\n";
    foreach my $img ($mech->find_all_images()) {
        my $img_url = $img->url();
        next unless $img_url =~ m{\.jpg$}i;    #只需要JPG图片
        next if $img_url =~ m{luo.*?\.jpg$}i;  #过滤不需要的论坛图片

        print "\t\t$img_url\n";
        my $img_file = $1 if $img_url =~ m{.*/(.*\.jpg)}i;
        push @{ $url{ $tid } }, $img_url;
    }
}

print "Now download jpg ...\n";

for my $i ( keys %url ) {
    print "[$i]\n";
    for my $url ( @{ $url{$i} } ) {
        $mutux->down;
        threads->create( "download", $url, $i)->detach;
    }
}

sub download()
{
    my ($img_url, $dir) = (@_);

    mkdir $dir unless -d $dir;
    my $img;
    ( $img = $img_url ) =~ s{.*/}{};

    print "$dir/$img -> ", $img_url, "\n";
    $mech->get($img_url, ':content_file' => $dir . "/" . $img);

    unlink "$dir/$img" unless $mech->success

    ## be polite
    sleep 1;

    ## release resource
    $mutux->up;
}
